/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata.api;

import android.content.Context;

import trs.wmata.BusStop;
import static trs.wmata.api.ApiXml.A_STRING;
import static trs.wmata.api.ApiXml.LAT;
import static trs.wmata.api.ApiXml.LON;
import static trs.wmata.api.ApiXml.NAME;
import static trs.wmata.api.ApiXml.ROUTES;
import static trs.wmata.api.ApiXml.STOP_ID;

import java.util.ArrayList;

import org.xml.sax.Attributes;

public final class BusStopSchedule extends ApiResponse {
    private final BusStop stop;
    private final ArrayList<String> routeCodes = new ArrayList<String>(10);

    public BusStopSchedule(final Context context) {
        stop = new BusStop(context);
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        // if (DIRECTIONNUM.equals(lName)) {
        // } else if (ENDTIME.equals(lName)) {

        /* } else */ if (LAT.equals(lName)) {
            doCaptureChars = true;
        } else if (LON.equals(lName)) {
            doCaptureChars = true;
        } else if (NAME.equals(lName)) {
            doCaptureChars = true;

            // } else if (ROUTEID.equals(lName)) {
            // } else if (ROUTES.equals(lName)) {
            // } else if (SCHEDULEARRIVALS.equals(lName)) {
            // } else if (SCHEDULETIME.equals(lName)) {
            // } else if (STARTTIME.equals(lName)) {
            // } else if (STOP.equals(lName)) {
        } else if (STOP_ID.equals(lName)) {
            doCaptureChars = true;

            // } else if (STOPSCHEDULEARRIVAL.equals(lName)) {
            // } else if (STOPSCHEDULEINFO.equals(lName)) {
            // } else if (TRIPDIRECTIONTEXT.equals(lName)) {
            // } else if (TRIPHEADSIGN.equals(lName)) {
            // } else if (TRIPID.equals(lName)) {
        } else if (A_STRING.equals(lName)) {
            doCaptureChars = true;
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        // if (DIRECTIONNUM.equals(lName)) {
        // } else if (ENDTIME.equals(lName)) {

        /* } else */ if (LAT.equals(lName)) {
            try {
                stop.setLat(Double.valueOf(buff.toString().trim()));
            } catch (final NumberFormatException e) {
                stop.setLat(38.8900);
            }

            doCaptureChars = false;
        } else if (LON.equals(lName)) {
            try {
                stop.setLon(Double.valueOf(buff.toString().trim()));
            } catch (final NumberFormatException e) {
                stop.setLon(-77.0300);
            }

            doCaptureChars = false;
        } else if (NAME.equals(lName)) {
            final String str = buff.toString().trim();
            stop.setName(str);
            doCaptureChars = false;

            // } else if (ROUTEID.equals(lName)) {
        } else if (ROUTES.equals(lName)) {
            stop.setRouteCodes(routeCodes.toArray(new String[routeCodes.size()]));
            doCaptureChars = false;

            // } else if (SCHEDULEARRIVALS.equals(lName)) {
            // } else if (SCHEDULETIME.equals(lName)) {
            // } else if (STARTTIME.equals(lName)) {
            // } else if (STOP.equals(lName)) {
        } else if (STOP_ID.equals(lName)) {
            final String str = buff.toString().trim();
            stop.setCode(str);
            doCaptureChars = false;

            // } else if (STOPSCHEDULEARRIVAL.equals(lName)) {
            // } else if (STOPSCHEDULEINFO.equals(lName)) {
            // } else if (TRIPDIRECTIONTEXT.equals(lName)) {
            // } else if (TRIPHEADSIGN.equals(lName)) {
            // } else if (TRIPID.equals(lName)) {
        } else if (A_STRING.equals(lName)) {
            final String str = buff.toString().trim();
            routeCodes.add(str);
            doCaptureChars = false;
        }
    }

    private boolean doCaptureChars = false;

    @Override
    public boolean doCaptureChars() {
        return doCaptureChars;
    }

    public BusStop getBusStop() {
        return stop;
    }
}
